#
# LuckyBot4, a python IRC luckybot
# (c) Copyright 2008 by Lucas van Dijk
# http://www.return1.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
#
# $Id: __init__.py 61 2009-03-30 23:25:17Z luckyluke56 $
#

import base64
from gettext import gettext as _
from luckybot.bot.plugins import Plugin

class AuthenticationPlugin(Plugin):

	PLUGIN_INFO = {
		'name': 'Authentication',
		'description': 'Plugin to authenticate bot admins',
		'authors': ['Lucas van Dijk'],
		'version': '1.0',
		'website': 'http://www.return1.net'
	}

	def initialize(self):
		self.register_command('login', self.do_login, help=_("Login with specified password"), args="password")
		self.register_command('logout', self.logout, help=_("Logout"))
		self.register_command('checklogin', self.check_login, help=_("Check if you're logged in"))
		self.register_message_handler(self.on_message)

	def do_login(self, event):
		password = base64.b64encode(event.message.bot_args)

		users = self.bot.settings.options('Admins')

		for nick in users:
			if event.user.nick.lower() == nick.lower() and password == self.bot.settings.get('Admins', nick):
				# Valid username/password
				if event.user.check_logged_in():
					event.user.send_notice(_("You're already logged in"))
					return
				else:
					event.user.login()
					event.user.send_notice(_("You've been succesfully logged in"))
					return

		event.user.send_notice(_("The password you entered is not correct"))

	def check_login(self, event):
		if event.user.check_logged_in():
			event.user.send_notice(_("You're logged in"))
		else:
			event.user.send_notice(_("You're not logged in"))

	def logout(self, event):
		event.user.logout()

	def on_message(self, event):
		if hasattr(event.message, 'command') and hasattr(event.message, 'nick'):
			if event.message.command == 'NICK':
				if self.bot.auth.check_logged_in(event.message.nick):
					self.bot.auth.log_out(event.message.nick)
					self.bot.auth.log_in(event.message.message)

			if event.message.command == 'QUIT':
				if self.bot.auth.check_logged_in(event.message.nick):
					self.bot.auth.log_out(event.message.nick)


